package cn.xspace.library.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.cn.wt.wtutils.AppManager;
import com.cn.wt.wtutils.log.LogUtils;
import com.ctnq.util.EncryptUtil;
import com.xspace.android.xafcommon.view.dialog.DialogControl;

import java.io.IOException;
import java.util.Map;

import cn.xspace.library.R;
import cn.xspace.library.util.EncryptBean;
import de.greenrobot.event.EventBus;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Wubj 创建于 2017/5/16.
 */
public class MyBaseActivity extends AppCompatActivity implements DialogControl {

    protected Context mContext;
    protected ProgressDialog mProgress;
    protected OkHttpClient client;//网络请求的client

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        mContext = this;
        mProgress = getWaitDialog(mContext, getString(R.string.loading));
//        mProgress = SuccinctProgress.getSuccinctProgress(mContext, getString(R.string.loading));
        mProgress.setCancelable(true);
        mProgress.setCanceledOnTouchOutside(false);

        getIntentData();
        initView();
        initData();
        initListener();
    }

    protected void getIntentData() {
    }

    /**
     * 初始化视图
     */
    protected void initView() {
    }

    protected void initData() {
        client = new OkHttpClient();
    }

    protected void initListener() {
    }

    @SuppressWarnings("unchecked")
    public <T> T $(int id) {
        return (T) this.findViewById(id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 针对RXJava专用同步网络请求
     *
     * @param url
     * @param inParam
     * @return
     */
    protected String initHttp(String url, Map<String, Object> inParam) {
        EncryptBean encryptBean = new EncryptBean(inParam);
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, encryptBean.sendData(encryptBean));
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        LogUtils.e("wangtao---->>>:网路请求地址:" + url);
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return EncryptUtil.getInstance().getDecryptData(response.body().string());
            } else {
                return "请求失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "请求失败";
    }

    /**
     * @param url
     * @param formBody
     */
    protected String initHttp(String url, FormBody formBody) {
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        LogUtils.e("wangtao---->>>:网路请求地址:" + url);
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                return "请求失败";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "请求失败";
    }

    public void onEventMainThread(Map<String, Object> inParam) {
    }

    public void showProgress(String text) {
        if (mProgress != null && !mProgress.isShowing()) {
            if (!TextUtils.isEmpty(text)) {
                setMessage(text);
            }
            mProgress.show();
        }
    }

    private void setMessage(String msg) {
        mProgress.setMessage(msg);
    }

    /**
     * 不显示圈圈
     */
    private void dismissProgress() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
        }
    }

    @Override
    public void hideWaitDialog() {
        dismissProgress();
//        SuccinctProgress.dismiss();
    }

    @Override
    public void showWaitDialog() {
        showProgress("");
//        SuccinctProgress.showSuccinctProgress(mContext, getString(R.string.loading));
    }

    @Override
    public void showWaitDialog(int resourcesId) {
        showProgress(getString(resourcesId));
//        SuccinctProgress.showSuccinctProgress(mContext, getString(resourcesId));
    }

    @Override
    public void showWaitDialog(String text) {
        showProgress(text);
//        SuccinctProgress.showSuccinctProgress(mContext, text);
    }

    public ProgressDialog getWaitDialog(Context context, String message) {
        ProgressDialog waitDialog = new ProgressDialog(context);
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message);
        }
        return waitDialog;
    }

    public OkHttpClient getClient() {
        return client;
    }
}
